﻿using Se.Model.Host;
using Se.Model.GMCommand;
using SeServer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Se.Model.Generator;
using Se.Protocol;
using Se.Protocol.GameModel.Chat;
using Se.Model.GameModel.Character;
using SeServer.Module;

namespace SeServer
{
    public class ProtocolController : IServerReceived
    {
        public ProtocolController()
        {
            InitGMCommand();
        }

        /// <summary>
        /// GM指令
        /// </summary>
        private GMCommandFactory _commandFactory;

        /// <summary>
        /// 初始化GM指令
        /// </summary>
        private void InitGMCommand()
        {
            this._commandFactory = new GMCommandFactory();
        }


        #region IServerReceived

        public void PtRequest_Login(SocketSession session, string userName, string pwd)
        {
            var accountController = (SeServer.Module.Character.AccountController)GameWorld.ModuleManager[ControllerType.Account];
            accountController.Login(session, userName, pwd);
        }

        public void PtRequest_Register(SocketSession session, string userName, string pwd)
        {
            var accountController = (SeServer.Module.Character.AccountController)GameWorld.ModuleManager[ControllerType.Account];
            accountController.RegistAccount(session, userName, pwd);
        }


        public void PtRequest_Chat(SocketSession session, int msgType, string message)
        {
            Console.WriteLine(message);
            if (string.IsNullOrEmpty(message)) return;
            if (!Enum.IsDefined(typeof(ChatType), msgType)) return;

            //GM指令
            var msgList = message.Split(' ');
            if (this._commandFactory.Manager.CommandMode == GMCommandModes.On && msgList.Length == GMCommandManager.c_GMCommand_MessageSplit_BaseLength && msgList[0].ToUpper() == GMCommandManager.c_GMCommand_Prefix)
            {
                this._commandFactory.Manager.ExecuteCommand(session, message);
                return;
            }

            List<ChatMsg> msgs = new List<ChatMsg>();
            ChatMsg.Builder msg = new ChatMsg.Builder();
            msg.SetMsg(message).SetName("");
            msgs.Add(msg.BuildPartial());
            PS_Send.UpdateChatMsg(session, msgs.ToArray());
        }

        #endregion


        public void PtRequest_ErrorInfo(SocketSession session, int errorType, string msg)
        {

        }


        public void PtRequest_CreatePlayer(SocketSession session, string name, int roleTypeID)
        {
            if (session.State != SessionState.LoginAccount || session.Account == null) return;
            var accountController = (SeServer.Module.Character.AccountController)GameWorld.ModuleManager[ControllerType.Account];


            var playerController = (SeServer.Module.Character.PlayerController)GameWorld.ModuleManager[ControllerType.Player];
            playerController.CreatePlayer(session, session.Account, name, roleTypeID);
        }
    }
}
